package com.zhaolin.contorller;

import com.zhaolin.entity.Question;
import com.zhaolin.entity.QuestionOpt;
import com.zhaolin.service.QuestionService;
import com.zhaolin.utils.MapControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/question")
@CrossOrigin
public class QuestionController {
    @Autowired
    QuestionService service;

    @PostMapping("create")
    @ResponseBody
    public Map<String,Object> create(@RequestBody Question question, HttpServletRequest request){
        List<QuestionOpt> options = question.getOptions();
        int i = service.create(question);
        if (i<=0){
            return MapControl.getInstance().error().getMap();
        }else{
            return MapControl.getInstance().add("id",i).getMap();
        }

    }
    @PostMapping("/query")
    @ResponseBody
    public Map<String, Object> query(@RequestBody Question question , ModelMap modelMap){
        List<Question> query = service.query(question);
        int count = service.count(question);
        return MapControl.getInstance().page(query,count).getMap();
    }

    @PostMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(int ids){
        int delete = service.delete(ids);
        if (delete>=0){
            return MapControl.getInstance().error().getMap();
        }else{
            return MapControl.getInstance().success().getMap();
        }

    }



}
